jQuery(function($){
	
	/**
	* Sur chaque div.player
	**/
	$('.player').each(function(){
		// On stock les différents éléments (pour éviter de rechercher dans le DOM plus tard)
		var e = $(this); 
		var audio =  new Audio();
		var progressBar = e.find('.progress span'); 
		var durationText = e.find('.duration span');
		var timeText = e.find('.time'); 
		var titleText = e.find('.title'); 
		var thumb = e.find('.thumb'); 
		var state = 0; 												// Permettra de connaitre l'état du player (0:aucune lecture - 1:lecture - 2:pause)

		// On test le support
		if (audio.canPlayType) {
			if(!audio.canPlayType('audio/mpeg')){
				e.find('.playlist a').each(function(){
					$(this).attr('href',$(this).attr('href').replace('.mp3','.ogg')); 
				});
			}
		}

		// On lit le premier élément
		play(e.find('.playlist a:first'));
		function play(link){
			durationText.text('Chargement en cours...'); 
			timeText.text('Chargement en cours...'); 
			link.addClass('active').siblings().removeClass('active'); 
			thumb.attr('src',link.find('img').attr('src')); 
			titleText.text(link.text()); 
			audio.src = link.attr('href'); 
		}

		// clique sur une piste de la playlist
		$('.playlist a').bind('click',function(){
			play($(this)); 
			audio.play(); 
			return false;
		});

		// Next
		function next(){
			var next = e.find('.playlist a.active').next('a:first'); 
			if(next.length>0){
				play(next); 
			}else{
				play(e.find('.playlist a:first')); 
			}
		}
		function prev(){
			var prev = e.find('.playlist a.active').prev('a:first');
			if(prev.length>0){
				play(prev); 
			}else{
				play(e.find('.playlist a:last')); 
			}
		}

		// Progression
		$(audio).bind('timeupdate',function(){
			var ratio = 100 * audio.currentTime / audio.duration;	// % d'écoulement
			progressBar.width(ratio+'%'); 							// On élargit la barre de progression
			var s =  Math.round(audio.duration%60); 				// On stock le nombre de seconde pour rajouter un 0 initial 
			if(s<10) s = '0'+s; 
			durationText.text(Math.floor(audio.duration/60) +':'+s);// On met à jour le texte indiquant la durée
			s = Math.round(audio.currentTime%60); 
			if(s<10) s = '0'+s;
			timeText.text(Math.floor(audio.currentTime/60) +':'+s); // On update le temps de progression
		}).bind('error',function(){
			var elem = e.find('.playlist a.active'); 
			next(); 
			elem.remove(); 
		}).bind('loadedmetadata',function(e){
			var s =  Math.round(audio.duration%60); 				// On stock le nombre de seconde pour rajouter un 0 initial 
			if(s<10) s = '0'+s;
			durationText.text(Math.floor(audio.duration/60) +':'+s);
			console.log(audio.data); 
			timeText.text('00:00'); 
		}).bind('ended',function(e){
			next(); 
			audio.play(); 
		});

		// Bouton Play 
		e.find('.play').bind('click',function(){
			audio.play(); 
			$(this).addClass('active').siblings().removeClass('active'); 
		}); 

		// Bouton Pause
		e.find('.pause').bind('click',function(){
			audio.pause(); 
			$(this).addClass('active').siblings().removeClass('active'); 
		}); 

		// Bouton next
		e.find('.next').bind('click',function(){
			next(); 
			audio.play(); 
		});
		// Bouton prev
		e.find('.prev').bind('click',function(){
			prev(); 
			audio.play(); 
		});

		// Clic pour avancer 
		e.find('.progress').bind('click',function(e){
			var p = $(this); 
			var mouseX = e.pageX - p.offset().left; // Position de la souris Par rapport à la barre
			var ratio = mouseX / p.width(); 		// Ratio 
			if(ratio > 1) ratio = 1; 
			audio.currentTime = audio.duration * ratio; 
		});

		// Gestion du volume
		e.find('.volume').slider({
			range: "min",
			value: 100,
			min: 0,
			max: 100,
			slide: function( event, ui ) {
				audio.volume = ui.value/100;
			}
		});


	});

});